package handler

import (
	"boy-go/middlewares"
	"boy-go/modules/system/service"
	"boy-go/modules/system/vo"
	"boy-go/pkg/response"
	"boy-go/pkg/xnum"
	"github.com/gin-gonic/gin"
	"strings"
)

// AddSysDictDataRouter 添加路由
func AddSysDictDataRouter(group *gin.RouterGroup) {
	group.GET("/dict/data/:id", middlewares.HasPermission("system:dict:query"), get_dict_data)
	group.GET("/dict/data/list", middlewares.HasPermission("system:dict:list"), list_dict_data)
	group.POST("/dict/data", middlewares.HasPermission("system:dict:add"), middlewares.Logger("字典数据", middlewares.INSERT), add_dict_data)
	group.PUT("/dict/data", middlewares.HasPermission("system:dict:edit"), middlewares.Logger("字典数据", middlewares.UPDATE), edit_dict_data)
	group.DELETE("/dict/data/:ids", middlewares.HasPermission("system:dict:remove"), middlewares.Logger("字典数据", middlewares.DELETE), del_dict_data)
	group.GET("/dict/data/type/:dictType", get_dict_data_type)
}

// get_dict_data
// @Summary 获取字典数据表详情
// @Description 获取字典数据表详情（管理页面）
// @Tags system-dict
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Success 200 {object} response.R[vo.SysDictDataModel] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/dict/{id} [get]
func get_dict_data(ctx *gin.Context) {
	var idStr = ctx.Param("id")
	id := xnum.Str2int64(idStr)
	m, err := service.NewSysDictDataService().GetById(id)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, m)
}

// list_dict_data
// @Summary 获取字典数据表列表
// @Description 获取字典数据表列表（管理页面）
// @Tags system-dict
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.SysDictDataModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/dict/list [get]
func list_dict_data(ctx *gin.Context) {
	req := vo.QuerySysDictDataReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	page, err := service.NewSysDictDataService().List(&req)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxDataTable(ctx, page.List, page.Total)
}

// add_dict_data
// @Summary 新增字典数据表
// @Description 新增字典数据表（管理页面）
// @Tags system-dict
// @Accept json
// @Produce json
// @Param req body  vo.AddSysDictDataReq true "req"
// @Success 200 {object} response.R[vo.AddSysDictDataRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/dict [post]
func add_dict_data(ctx *gin.Context) {
	var req vo.AddSysDictDataReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxFailCode(ctx, response.ParamError)
		return
	}
	m, err := service.NewSysDictDataService().Add(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// edit_dict_data
// @Summary 编辑字典数据表
// @Description 编辑字典数据表（管理页面）
// @Tags system-dict
// @Accept json
// @Produce json
// @Param req body  vo.EditSysDictDataReq true "req"
// @Success 200 {object} response.R[vo.EditSysDictDataRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/dict [put]
func edit_dict_data(ctx *gin.Context) {
	var req vo.EditSysDictDataReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxFailCode(ctx, response.ParamError)
		return
	}
	m, err := service.NewSysDictDataService().Edit(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// del_dict_data
// @Summary 删除字典数据表
// @Description 删除字典数据表（管理页面）
// @Tags system-dict
// @Accept json
// @Produce json
// @Success 200 {object} response.R[string] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /system/dict [delete]
func del_dict_data(ctx *gin.Context) {
	var idsStr = ctx.Param("ids")
	ids := strings.Split(idsStr, ",")
	err := service.NewSysDictDataService().BatchDel(ids)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, nil)
	}
}

func get_dict_data_type(ctx *gin.Context) {
	var dictType = ctx.Param("dictType")
	datas, err := service.NewSysDictTypeService().SelectDictDataByType(dictType)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, datas)
	}
}
